using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Web.Services;
using System.Web;
using System.Xml;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2004-08-10
	Status:		live
	Version:	1.0.2
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.workflowX {
	/// <summary>
	/// This class describes an item of work within a workflow process. 
	/// <para>Workitems are persisted in the datastore and retrived by this class.</para>
	/// <para>Each work-item is described by a number of public properties, including the 'Tasks', 'Process' and 'Request' Xml elements</para>
	/// <para></para>
	/// </summary>
	public class x_workitem_custom : x_workitem {
		#region Constants
		#endregion

		#region Invisible properties
		//private XmlDocument workitem;
		#endregion

		#region Visible properties
		//private int id;
		#endregion

		#region Constructors/Destructors
		/// <overloads>work-item constructor</overloads>
		/// <summary>Initialises the datastore connection string</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		public x_workitem_custom(string DSN) : base(DSN) {
			initialise();
		}
		/// <summary>Retrieves the work-item from the datastore</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		/// <param name="workitemID">The identifier of the work-item</param>
		/// <exception cref="x_exception">Cannot find a work-item for the given identifier</exception>
		public x_workitem_custom(string DSN, int workitemID) : base(DSN) {
			initialise();
			Get(workitemID);
		}
        #endregion

        #region Public methods
        /// <overloads>Create</overloads>
        ///	<summary>
        ///	Creates a work-item and saves it into the database
        ///	</summary>
        /// <param name="ProcessID">The process identifier</param>
        /// <param name="AgentID">The Agent identifier</param>
        /// <param name="UserID">The User identifier</param>
        /// <param name="UserName">The User's full name</param>
        /// <param name="UserContact">The User's contact number</param>
        /// <param name="UserEmail">The User's email address</param>
        /// <param name="Request">The request description / title</param>
        /// <param name="RequestXml">An xml formatted string of the request data</param>
        /// <param name="customWorkItem">Work item custom data specifier</param>
        /// <param name="ExpDate">Deal expiry date</param>
        /// <exception cref="x_exception">error_request: 
        /// <para>There is a error in the validity of the request Xml</para>
        /// </exception>
        public void Create(int ProcessID, int AgentID, int UserID, string UserName, string UserContact, string UserEmail, string Request, string RequestXml, string customWorkItem, string ExpDate) {
            save(ProcessID, AgentID, UserID, UserName, UserContact, UserEmail, Request, RequestXml);
            IDataParameter parID = _addDbParameter("@ID");
            _executeNonQuery();
            ID = (int)_getParameter(parID);
            // add additional custom data for eg sales leads
            _getCommand("xWorkitemAppend" + customWorkItem, CommandType.StoredProcedure);
            _addDbParameter(ID, "@WorkItemID");
            _addDbParameter(ExpDate, "@ExpDate", 50);
            _executeNonQuery();
            // now start the new process
            start();
        }
        
        /// <overloads>Create</overloads>
        ///	<summary>
        ///	Creates a work-item and saves it into the database
        ///	</summary>
        /// <param name="ProcessID">The process identifier</param>
        /// <param name="AgentID">The Agent identifier</param>
        /// <param name="UserID">The User identifier</param>
        /// <param name="UserName">The User's full name</param>
        /// <param name="UserContact">The User's contact number</param>
        /// <param name="UserEmail">The User's email address</param>
        /// <param name="Request">The request description / title</param>
        /// <param name="RequestXml">An xml formatted string of the request data</param>
        /// <param name="customWorkItem">Work item custom data specifier</param>
        /// <param name="ProcTypID">Process type identifier</param>
        /// <param name="ProcCatID">Process type category identifier</param>
        /// <param name="FranchiseID">Franchise identifier</param>
        /// <param name="ProcTyp">Process type</param>
        /// <param name="ProcCat">Process type category</param>
        /// <param name="Franchise">Franchise name</param>
        /// <param name="RefNo">refference number of task</param>
        /// <exception cref="x_exception">error_request: 
        /// <para>There is a error in the validity of the request Xml</para>
        /// </exception>
        public void Create(int ProcessID, int AgentID, int UserID, string UserName, string UserContact, string UserEmail, string Request, string RequestXml, string customWorkItem, int ProcTypID, int ProcCatID, int FranchiseID, string ProcTyp, string ProcCat, string Franchise, string RefNo, string LoggedBy) {
            
            save(ProcessID, AgentID, UserID, UserName, UserContact, UserEmail, Request, RequestXml);
            IDataParameter parID = _addDbParameter("@ID");
            _executeNonQuery();
            ID = (int)_getParameter(parID);
            // add additional custom data for eg sales leads
            _getCommand("xWorkitemAppend" + customWorkItem, CommandType.StoredProcedure);
            _addDbParameter(ID, "@WorkItemID");
            _addDbParameter(ProcTypID, "@ProcessTypeID");
            _addDbParameter(ProcCatID, "@ProcessCategoryID");
            _addDbParameter(FranchiseID, "@FranchiseID");
            _addDbParameter(ProcTyp, "@ProcessType", 100);
            _addDbParameter(ProcCat, "@ProcessCategory", 50);
            _addDbParameter(Franchise, "@Franchise", 50);
            _addDbParameter(RefNo, "@ReferenceNo", 20);
            _addDbParameter(LoggedBy, "@LoggedBy", 50);
            _executeNonQuery();
        }
        ///	<summary>
        ///	Extends Complete 
        ///	</summary>
        /// <param name="workitemID">The identifier of the work-item</param>
        /// <param name="comment">A comment to be associated with the completed task</param>
        /// <param name="personID">An person identifier (could be agent, user or owner)</param>
        /// <param name="branch">Branch switch (yes/no)</param>
        /// <param name="callerID">The identifier of the caller</param>
        /// <param name="dealno">The identifier of the deal</param>
        /// <param name="stateID">state identifier</param>
        /// <exception cref="x_exception">No tasks are associated with the current work-item</exception>
        /// <exception cref="x_exception">There is not current task associated with the current work-item</exception>
        public void CompleteSales(int workitemID, string comment, int personID, string branch, string callerID, string dealno, int stateID) {
            // Set state
            do_setstate = true;
            stateid = stateID;
            //Get current AgentID
            Get(workitemID);
            int curr_agent = AgentID;
            //Use personID as agent
            Complete(workitemID, comment, personID, branch);
            // add additional custom data for eg sales leads
            _getCommand("xWorkitemUpdateSales", CommandType.StoredProcedure);
            _addDbParameter(workitemID, "@WorkItemID");
            _addDbParameter(callerID, "@CallerID", 50);
            _addDbParameter(dealno, "@DealNo", 50);
            _executeNonQuery();
            //Re-assign old agent
            ReassignSilent(workitemID, curr_agent);
        }
        ///	<summary></summary>
        /// <param name="workitemID">The identifier of the work-item</param>
        /// <param name="comment">A comment to be associated with the completed task</param>
        /// <param name="personID">An person identifier (could be agent, user or owner)</param>
        /// <param name="branch">Branch switch (yes/no)</param>
        /// <param name="issue_type">Issue type</param>
        /// <param name="issue">Issue</param>
        /// <exception cref="x_exception">No tasks are associated with the current work-item</exception>
        /// <exception cref="x_exception">There is not current task associated with the current work-item</exception>
        public void CompleteHelpdesk(int workitemID, string comment, int personID, string issue_type, string issue) {
            Complete(workitemID, comment, personID);
            // add additional custom data for eg sales leads
            _getCommand("xWorkitemUpdateHelpdesk", CommandType.StoredProcedure);
            _addDbParameter(workitemID, "@WorkItemID");
            _addDbParameter(issue_type, "@IssueType", 50);
            _addDbParameter(issue, "@Issue", 50);
            _executeNonQuery();
        }
		///	<summary>
		///	Extends Complete 
		///	</summary>
		/// <param name="workitemID">The identifier of the work-item</param>
		/// <param name="comment">A comment to be associated with the completed task</param>
		/// <param name="personID">An person identifier (could be agent, user or owner)</param>
		/// <param name="branch">Branch switch (yes/no)</param>
		/// <param name="callerID">The identifier of the caller</param>
		/// <param name="dealno">The identifier of the deal</param>
		/// <exception cref="x_exception">No tasks are associated with the current work-item</exception>
		/// <exception cref="x_exception">There is not current task associated with the current work-item</exception>
		public void CompleteSales(int workitemID, string comment, int personID, string branch, string callerID, string dealno) {
			//Get current AgentID
			Get(workitemID);
			int curr_agent = AgentID;
			//Use personID as agent
			Complete(workitemID, comment, personID, branch);
			// add additional sales data
			_getCommand("xWorkitemUpdateSales", CommandType.StoredProcedure);
			_addDbParameter(ID, "@WorkItemID");
			_addDbParameter(callerID, "@CallerID", 50);
			_addDbParameter(dealno, "@DealNo", 50);
			_executeNonQuery();
			//Re-assign old agent to task
			ReassignSilent(workitemID, curr_agent);
		}
		#endregion
				
		#region Protected methods
		#endregion
				
		#region Private methods
		#endregion

	}
}
